/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package course.ya.doc.from.code.model.ast;

import course.ya.doc.from.code.model.statements.AStatement;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 *
 * @author def
 */
@Entity
@Table(name = "nodes")
@SequenceGenerator(sequenceName = "global_seq", name = "global_seq")
public class StatementTreeNode implements Serializable {

    private static final long serialVersionUID = -985842882752L;

    @Id
    @Column(name = "node_id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private long id;

    @OneToOne(cascade = CascadeType.REMOVE)
    @JoinColumn(name = "statement_id")
    private AStatement statement;

    @ManyToOne
    @JoinColumn(name = "parent_id")
    private StatementTreeNode parent;

    @OneToMany(cascade = CascadeType.REMOVE)
    @JoinColumn(name = "parent_id")
    private List<StatementTreeNode> children;

    public StatementTreeNode() {};

    public StatementTreeNode(AStatement st, StatementTreeNode parent) {
        this.statement = st;
        this.parent = parent;
        this.children = new ArrayList<StatementTreeNode>();
    }

    public AStatement getStatement() {
        return statement;
    }

    public StatementTreeNode getParent() {
        return parent;
    }

    public List<StatementTreeNode> getChildren() {
        return children;
    }

    public void setStatement(AStatement statement) {
        this.statement = statement;
    }

    public void setParent(StatementTreeNode parent) {
        this.parent = parent;
    }

    public void setChildren(List<StatementTreeNode> children) {
        this.children = children;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null && !(obj instanceof StatementTreeNode)) {
            return false;
        }

        StatementTreeNode otherNode = (StatementTreeNode) obj;

        if (this.statement == null) {
            if (otherNode.statement != null) {
                return false;
            }
        } else {
            if (!this.getStatement().equals(otherNode.getStatement())) {
                return false;
            }
        }

        if (this.children == null) {
            if (otherNode.children != null) {
                return false;
            }
        } else {
            if (otherNode.children == null) {
                return false;
            }
            if ((this.children.size() != otherNode.children.size())) {
                return false;
            }
        }

        for (int i = 0; i < this.children.size(); i++) {
            if (!this.children.get(i).equals(otherNode.children.get(i))) {
                return false;
            }
        }
        return true;
    }
}
